Leaflet

Leaflet is javascript library. You can access it, like many other cool interactive libraries (i.e., dygraphs) thanks to the work of people in RStudio. Here, I’ll layout a basic structure for plotting simple maps in leaflet and then how to advance them and plot chorophlets.

Inspired by:

Loading packages

library(tidyverse)
## ── Attaching packages ────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 2.2.1     ✔ purrr   0.2.4
## ✔ tibble  1.4.2     ✔ dplyr   0.7.4
## ✔ tidyr   0.8.1     ✔ stringr 1.3.1
## ✔ readr   1.1.1     ✔ forcats 0.3.0
## ── Conflicts ───────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(leaflet)
library(raster)
## Loading required package: sp
## 
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
## 
##     select
## The following object is masked from 'package:tidyr':
## 
##     extract

Getting the polygons

A basic map will be one with the first level political divisions of a country drawn. To do so, we need a set of polygons for the whole country and the poilitical divisions. The easiest way to get these political divisions is with raster::getData. Let’s do so for Colombia

getData("ISO3") %>% 
  filter(startsWith(NAME, "Colomb"))
##   ISO3     NAME
## 1  COL Colombia
colombia <- getData(name = "GADM", country = "COL", level = 1)
class(colombia)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"

Drawing with Leaflet

Now, let’s draw them! A basic leaflet structure, simply change the data argument.

leaflet() %>% 
addProviderTiles("OpenStreetMap.Mapnik") %>%
setView(lng = -74.063, 4.62, 4) %>%
addPolygons(data = colombia,
            stroke = FALSE, smoothFactor = 0.2, fillOpacity = 0.3,
            highlight = highlightOptions(
    weight = 5,
    color = "#666",
    dashArray = "",
    fillOpacity = 0.7,
    bringToFront = TRUE),
    label = colombia$NAME_1,
labelOptions = labelOptions(
    style = list("font-weight" = "normal", padding = "3px 8px"),
    textsize = "15px",
    direction = "auto"))

Chorophlets

To make a chorophlet, we have to add another argument to the leaflet::addPolygons() function:

tbl <- data.frame(place = unique(colombia$NAME_1),
                   value = sample.int(n = 10000, size = n_distinct(colombia$NAME_1), replace = TRUE))

mypal <- colorNumeric(palette = "viridis", domain = tbl$value, n = 5, reverse = TRUE)

labels <- sprintf(
  "<strong>%s</strong><br/>%g ",
  tbl$place, tbl$value
) %>% lapply(htmltools::HTML)

leaflet() %>% 
addProviderTiles("OpenStreetMap.Mapnik") %>%
setView(lng = -74.063, 4.62, 4) %>%
addPolygons(data = colombia,
            stroke = FALSE, smoothFactor = 0.2, fillOpacity = 0.3,
            highlight = highlightOptions(
    weight = 5,
    color = "#666",
    dashArray = "",
    fillOpacity = 0.7,
    bringToFront = TRUE),
    fillColor = ~mypal(tbl$value),
    label = labels,
labelOptions = labelOptions(
    style = list("font-weight" = "normal", padding = "3px 8px"),
    textsize = "15px",
    direction = "auto")) %>%
addLegend(position = "bottomright", pal = mypal, values = tbl$value,
          title = "# of ",
          labFormat = labelFormat(prefix = "$"),
          opacity = 1)

Notice that there are multiple color functions. Use them wisely according to your need.